package com.threeox.commonlibrary.adapter.base;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.threeox.commonlibrary.CommonApplcation;
import com.threeox.commonlibrary.util.ViewUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @ClassName: BaseViewHolder 
 *
 * @Description: TODO() 
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016-9-13 上午11:33:34 
 *
 */
public class BaseViewHolder {

	private static final String ViewHodler = null;
	private List<Object> mTags;
	private SparseArray<View> mViews;
	private Map<Object, View> mTagViews;
	private int mPosition;
	private View mConvertView;
	// 是否为第一次加载
	private boolean isFirstLoad = true;

    private ViewUtils mViewUtils;

	private BaseViewHolder(Context context, ViewGroup parent, int layoutId,
			int position) {
		this.mPosition = position;
		mViews = new SparseArray<View>();
		mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
		mConvertView.setTag(this);
		isFirstLoad = true;
        mViewUtils = ViewUtils.newInstance();
    }

	/*private BaseViewHolder(Context context, ViewGroup parent, int layoutId, int position, SlideView slideView) {
		this.mPosition = position;
		mViews = new SparseArray<View>();
		View itemView = BaseUtils.inflate(context, layoutId);
		slideView.setContentView(itemView);
		mConvertView.setTag(this);
		mConvertView = slideView;
		isFirstLoad = true;
	}*/

	/**
	 * 得到ViewHolder
	 * 
	 * @param context
	 * @param convertView
	 * @param parent
	 * @param layoutId
	 * @param position
	 * @return
	 */
	public static BaseViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
		if (convertView == null) {
			return new BaseViewHolder(context, parent, layoutId, position);
		} else {
			BaseViewHolder hodler = (BaseViewHolder) convertView.getTag();
			hodler.mPosition = position;
			hodler.isFirstLoad = false;
			return hodler;
		}
	}
	
	/**
	 * 得到ViewHolder 使用SlideView包裹
	 * 
	 * @param context
	 * @param convertView
	 * @param parent
	 * @param layoutId
	 * @param position
	 * @return
	 */
	/*public static BaseViewHolder getSlideView(Context context,
			View convertView, ViewGroup parent, int layoutId, int position) {
		if (convertView == null) {
			return new BaseViewHolder(context, parent, layoutId, position,
					new SlideView(context));
		} else {
			BaseViewHolder hodler = (BaseViewHolder) convertView.getTag();
			hodler.mPosition = position;
			hodler.isFirstLoad = false;
			return hodler;
		}
	}*/

	/**
	 * 得到ConvertView
	 * 
	 * @return
	 */
	public View getConvertView() {
		return mConvertView;
	}

	/**
	 * 得到View
	 * 
	 * @param viewId
	 * @return
	 */
	public <T extends View> T getView(int viewId) {
		View view = mViews.get(viewId);
		if (view == null) {
			view = mConvertView.findViewById(viewId);
			mViews.put(viewId, view);
		}
		return (T) view;
	}
	
	/**
	 * 根据Tag得到View
	 * 
	 * @param tag
	 * @return
	 */
	public <T extends View> T getView(Object tag) {
		if (mTagViews == null) {
			mTagViews = new HashMap<Object, View>();
		}
		View view = mTagViews.get(tag);
		if (view == null) {
			view = mConvertView.findViewWithTag(tag);
			mTagViews.put(tag, view);
		}
		return (T) view;
	}

	/**
	 * 得到所有View的Tag
	 *
	 * @return
     */
	public List<Object> getViewTags(){
		if (mTags == null) {
			mTags = new ArrayList<Object>();
		} else {
			return mTags;
		}
		if (mConvertView != null && mConvertView instanceof ViewGroup) {
			mTags.addAll(ViewUtils.getViewTags((ViewGroup) mConvertView));
		} 
		return mTags;
	}
	
	/**
	 * 设置文本
	 * 
	 * @param viewId
	 * @param text
	 * @return
	 */
	public BaseViewHolder setText(int viewId, CharSequence text) {
		return setText(null, viewId, text);
	}

	public BaseViewHolder setText(View v, int viewId, CharSequence text) {
		TextView view = null;
		if (v != null) {
			view = (TextView) v.findViewById(viewId);
		} else {
			view = getView(viewId);
		}
		view.setText(text);
		return this;
	}

	/**
	 * 设置图片ResourId
	 * 
	 * @param viewId
	 * @param drawId
	 * @return
	 */
	public BaseViewHolder setBackgroundResource(int viewId, int drawId) {
		((ImageView) getView(viewId)).setBackgroundResource(drawId);
		return this;
	}

	/**
	 * 为ImageView设置图片
	 * 
	 * @param viewId
	 * @param drawableId
	 * @return
	 */
	public BaseViewHolder setImageResource(int viewId, int drawableId) {
		ImageView view = getView(viewId);
		view.setImageResource(drawableId);
		return this;
	}

	/**
	 * 设置btn 图片和文字
	 *
	 * @param viewId
	 * @param drawId
	 * @param msg
     * @return
     */
	public BaseViewHolder setBtnMsg(int viewId, int drawId, CharSequence msg) {
		// TODO Auto-generated method stub
		Button btn = getView(viewId);
		btn.setText(msg);
		Drawable draw = CommonApplcation.getInstance().getResources()
				.getDrawable(drawId);
		draw.setBounds(0, 0, draw.getMinimumWidth(), draw.getMinimumHeight());
		btn.setCompoundDrawables(null, draw, null, null);
		return this;
	}

	/**
	 * 设置显示状态
	 * 
	 * @param viewId
	 * @param visibility
	 * @return
	 */
	public BaseViewHolder setVisibility(int viewId, int visibility) {
		View view = getView(viewId);
		if (view != null) {
			view.setVisibility(visibility);
		}
		return this;
	}

	/**
	 * 设置点击事件
	 *
	 * @param viewId
	 * @param click
     * @return
     */
	public BaseViewHolder setClick(int viewId, OnClickListener click) {
		return setClick(null, viewId, click);
	}

	/**
	 * 设置点击事件
	 *
	 * @param v
	 * @param viewId
	 * @param click
     * @return
     */
	public BaseViewHolder setClick(View v, int viewId, OnClickListener click) {
		View view = getView(viewId);
		if (v != null) {
			view = v.findViewById(viewId);
		}
		view.setOnClickListener(click);
		return this;
	}

	/**
	 * 设置选中事件
	 * 
	 * @param viewId
	 * @param checked
	 * @return
	 */
	public BaseViewHolder setChecked(int viewId, boolean checked) {
		CheckBox _CheckBox = getView(viewId);
		if (_CheckBox != null) {
			_CheckBox.setChecked(checked);
		}
		return this;
	}

	/**
	 * 得到是否被选中
	 *
	 * @param viewId
	 * @return
     */
	public boolean isChecked(int viewId) {
		CheckBox _CheckBox = getView(viewId);
		if (_CheckBox != null) {
			return _CheckBox.isChecked();
		}
		return false;
	}


	/**
	 * 设置值
	 * 
	 * @param viewId
	 * @param val
	 * @return
	 */
	public BaseViewHolder setValue(int viewId, Object val) {
		View view = getView(viewId);
        mViewUtils.setValue(view, val);
		return this;
	}

	public boolean isFirstLoad() {
		return isFirstLoad;
	}

}